package com.itheima.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.R;
import com.itheima.pojo.AddressBook;
import com.itheima.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService bookService;

    /**
     * 新增地址
     * @param addressBook
     * @param session
     * @return
     */
    @PostMapping
    public R<String> saveAddressBook(@RequestBody AddressBook addressBook, HttpSession session){
        Long userId = (Long) session.getAttribute("userId");
        addressBook.setUserId(userId);
        log.info("addressBook:{}", addressBook);
        bookService.save(addressBook);
        return R.success("保存地址成功");
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @param session
     * @return
     */
    @PutMapping("/default")
    public R<String> updateDefault(@RequestBody AddressBook addressBook, HttpSession session){
        Long userId = (Long) session.getAttribute("userId");
        //根据用户id先把用户所有地址更新为0
        LambdaUpdateWrapper<AddressBook> lqw = new LambdaUpdateWrapper<>();
        lqw.set(AddressBook::getIsDefault,0);
        lqw.eq(AddressBook::getUserId,userId);
        bookService.update(lqw);
        //然后将当前的地址设置的默认地址的is_default设置为1
        addressBook.setIsDefault(1);
        bookService.updateById(addressBook);
        return R.success("设置成功");
    }

    /**
     *  根据ID查询地址
     * @param addressBookId
     * @return
     */
    @GetMapping("/{addressBookId}")
    public R<AddressBook> selectAddressBookById(@PathVariable Long addressBookId){
        AddressBook addressBook = bookService.getById(addressBookId);
        if (addressBook != null) {
            return R.success(addressBook);
        }
        return R.error("没有找到该对象");
    }

    /**
     * 查询默认地址
     * @param session
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> selectDefaultAddressBook(HttpSession session){
        Long userId = (Long) session.getAttribute("userId");
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getIsDefault,1);
        lqw.eq(AddressBook::getUserId,userId);
        AddressBook addressBook = bookService.getOne(lqw);
        if (addressBook != null) {
            return R.success(addressBook);
        }
        return R.error("没有找到该对象");
    }

    /**
     * 查询当前用户所有地址
     * @param session
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> selectAddressBookAll(HttpSession session){
        Long userId = (Long) session.getAttribute("userId");
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId,userId);
        List<AddressBook> addressBooks = bookService.list(lqw);
        return R.success(addressBooks);
    }

    /**
     * (批量)删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteAddressBook(@RequestParam List<Long> ids){
        bookService.removeByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook){
        bookService.updateById(addressBook);
        return R.success("修改成功");
    }
}
